package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-08-17
 * Time: 9:53
 */
public class Demo8 {
    //写做成员变量就不是触发变量捕获的逻辑了，而是“内部类访问外部类的成员”，本身就是OK的。
    public static boolean isQuit = false;
    public static void main(String[] args) throws InterruptedException {
        //boolean isQuit = false;
        Thread t = new Thread(()->{
           while (!isQuit) {
               System.out.println("hello world");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });
        t.start();

        //主线程执行一些其他逻辑后，要让 t 线程结束.
        Thread.sleep(3000);

        //这个代码就是在修改前面设置的标志位.
        isQuit = true;
        System.out.println("把 t 线程中断");
    }
}
